#include <stdio.h>
#include <string>
#include <list>

int table[255];
int numbers[] = {57, 71, 87, 97, 99, 101, 103, 113, 113, 114, 115, 128, 128, 129, 131, 137, 147, 156, 163, 186 };
int* results;

int str2int(std::string str)
{
	int result = 0;
	for (auto it=str.begin(); it!=str.end(); it++)
	{
		char ch = *it;
		result += table[ch];
	}
	return result;
}

int main()
{
	FILE* file = fopen("stepic_dataset.txt", "r");
	FILE* table_file = fopen("integer_mass_table.txt", "r");
	FILE* out = fopen("result.txt", "w");
	char* str = new char[100];
	fscanf(file, "%s", str);
	std::string input(str);
	int size = input.size();

	for (int i=0; i<20; i++)
	{
		char c[15];
		int num;
		fscanf(table_file, "%s %d", &c, &num);
		table[c[0]] = num;
	}
	std::list<int> mylist;
	std::reverse(input.begin(), input.end());
	std::string dbl_str = input+input;
	for (int i=1; i<size; i++)
	{
		for (int j=0; j<size; j++)
		{
			std::string cur = dbl_str.substr(j, i);
			mylist.push_back(str2int(cur));
		}
	}
	mylist.push_back(0);
	mylist.push_back(str2int(input));
	mylist.sort();
	for (auto it=mylist.begin(); it!=mylist.end(); it++)
		fprintf(out, "%d ", *it);
	printf("\n");
	return 0;
}